class Solution {
public:
    int smallestNumber(int n) {
        vector<int> ni;
        bool zero = false;
        int tmp = n;
        while (n) {
            if (n % 2 == 0) zero = true;
            ni.push_back(n % 2);
            n /= 2;
        }

        int res = 0;
        if (zero) {
            for (int i = ni.size() - 1; i >= 0; i--) {
                res *= 2;
                res += 1;
            }
        }
        else {
            res = tmp;
        }
        return res;
    }
};